//
//  KANavView.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit
import SnapKit


enum kaEnumNavBarType {
    case normal //show back
    case root //hide back
    case clear //clear
    case center //show back，title left
}

class KANavView: UIView {
    
    var BLOCK_gobackBlock : (() -> Void)? = nil
    
    var var_type : kaEnumNavBarType = .normal
    
    var var_searchBarH = 32.0
    
    lazy var var_backBtn : UIView = {
        let var_backBtn = UIView(frame: CGRect(x: 8, y: bounds.size.height-45, width: 40, height: 40))
        var_backBtn.isUserInteractionEnabled = true
        var_backBtn.addSubview(var_btn)
        var_btn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 36, height: 36))
        }
        let var_tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ka_gobackClick))
        var_backBtn.addGestureRecognizer(var_tap)
        return var_backBtn
    }()
    
    @objc func ka_gobackClick() {
        if BLOCK_gobackBlock != nil {
            BLOCK_gobackBlock!()
        }
    }
    
    lazy var var_btn : UIButton = {
        let var_btn = UIButton()
        var_btn.isUserInteractionEnabled = false
        var_btn.setImage(UIImage(named: "nav_goBack"), for: .normal)
        return var_btn
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.textColor = .black
        var_titleLabel.font = UIFont.systemFont(ofSize: kscale(18), weight: .medium)
        var_titleLabel.textAlignment = .center
        var_titleLabel.isHidden = false
        return var_titleLabel
    }()
    
    lazy var var_rightBtn: UIButton = {
        let var_rightBtn = UIButton()
        var_rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(14))
        var_rightBtn.isHidden = true
        return var_rightBtn
    }()
    
    
    convenience init(_ type : kaEnumNavBarType) {
        self.init()
        
        frame = CGRect(x: 0, y: 0, width: var_screenWidth, height: UIDevice.ka_fullNavHeight)
        backgroundColor = .colorFFF
        isUserInteractionEnabled = true
        
        var_type = type
        ka_setSubViews()
    }
    
    func ka_setSubViews() {
        addSubview(var_backBtn)
        addSubview(var_titleLabel)
        addSubview(var_rightBtn)
        
        var_rightBtn.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.height.equalTo(35)
            make.width.greaterThanOrEqualTo(35).priority(.low)
//            make.size.mas_equalTo(CGSizeMake(35, 35));
            make.centerY.equalTo(var_backBtn).offset(-2)
        }
        var_titleLabel.snp.makeConstraints { make in
//            make.centerX.equalTo(self);
            make.centerY.equalTo(var_backBtn);
            make.left.equalTo(var_backBtn.snp.right).offset(12)
        }
        
        if var_type == .root {
            var_backBtn.isHidden = true
        } else if var_type == .normal {
            var_backBtn.isHidden = false
        } else if var_type == .clear {
            backgroundColor = .clear;
            var_backBtn.isHidden = true
            var_titleLabel.text = "";
        } else if var_type == .center {
            var_backBtn.isHidden = false
            var_titleLabel.snp.remakeConstraints { make in
                make.centerX.equalTo(self);
                make.centerY.equalTo(var_backBtn);
            }
        }

    }
    
    func ka_gobackBlock(_ block : @escaping () -> ()) {
        BLOCK_gobackBlock = block
    }
    
    func ka_setupTransparent() {
        backgroundColor = .clear;
        var_backBtn.isHidden = true;
        var_titleLabel.text = "";
    }
    
    func updateUI(alpha: Float) {
        self.backgroundColor = UIColor.ka_hexColor(0xFFFFFF, alphaVaule: alpha)
    }
}
